package com.example.rabbitmqspringboot.demos.web.controller;

import com.example.rabbitmqspringboot.demos.web.pojo.Stud;
import com.example.rabbitmqspringboot.demos.web.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName: SendMsgController
 * @Description:
 * @Date 2024/03/28 09:14
 * @Author songshaoqiu
 **/
@RestController
@Slf4j
public class SendMsgController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("/send")
    public String consumer(@RequestParam("num") Integer num){

        for (int i = 0; i < num; i++) {
            if (i%2==0){
                User user = new User();
                user.setName("张三"+i);
                user.setAge(14);
                try {
                    rabbitTemplate.convertAndSend("hello-java-directExchange", "hello-java", user);
                }catch (Exception e){
                    log.info("消息发送失败");
                    e.printStackTrace();
                }
            }else{
                Stud stud = new Stud();
                stud.setStudName("学生aaaaaaaaaaaaaaaa"+i);
                stud.setAge("65636565");
                try {
                    rabbitTemplate.convertAndSend("hello-java-directExchange", "hello-java", stud);
                }catch (Exception e){
                    log.info("消息发送失败");
                    e.printStackTrace();
                }
            }

            log.info("消息发送成功【{}】");
        }
        return "发送成功";
    }


    @GetMapping("/sendToStud")
    public String sendToStud(){
        Stud stud = new Stud();
        stud.setStudName("发送给学生消息");
        stud.setAge("999");
        try {
            rabbitTemplate.convertAndSend("hello-java-directExchange", "hello-java", stud);
        }catch (Exception e){
            log.info("消息发送失败");
            e.printStackTrace();
        }
        return "1";
    }
    /**
     * 延迟队列
     * @return
     */
    @GetMapping("/test/order")
    public String testOrder(){
        Stud stud = new Stud();
        stud.setAge("11");
        stud.setStudName("adgads");
        rabbitTemplate.convertAndSend("order-event-exchange","order.create.order",stud);
        return "发送成功";
    }



    @GetMapping("/testStudDead")
    public String testStudDead(){
        Stud stud = new Stud();
        stud.setAge("888");
        stud.setStudName("孙权");
        rabbitTemplate.convertAndSend("formal_exchange","formal_rounte_key",stud);
        return "1";
    }


    @GetMapping("/myDeadTest")
    public String myDeadTest(){
        Stud stud = new Stud();
        stud.setAge("888");
        stud.setStudName("孙权");
        try {
            rabbitTemplate.convertAndSend("studExchange","publish-stud-msg",stud);
        }catch (Exception e){
            log.error("发消息失败");
            e.printStackTrace();
        }
        return "1";
    }
}
